<template>
  <div class="app-container">
    <!-- 搜索框&新增案例 -->
    <div class="search-container">
      <!-- <el-form :inline="true">
        <el-form-item>
          <el-input
            prefix-icon="el-icon-search"
            type="text"
            style="width: 500px"
            placeholder="请输入关键词..."
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="search-button">搜索</el-button>
        </el-form-item>
      </el-form> -->
      <el-form v-if="checkPermission(['企业导师', '校内老师'])">
        <el-form-item>
          <router-link to="/case/create">
            <el-button type="primary">新增案例</el-button>
          </router-link>
        </el-form-item>
      </el-form>
    </div>
    <!-- 案例列表 -->
    <div>
      <el-table
        :data="list"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="序号" min-width="30%" align="center">
          <template slot-scope="scope">
            {{ scope.$index+1 }}
          </template>
        </el-table-column>
        <el-table-column label="名称" min-width="50%" align="center">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="类别" min-width="40%" align="center">
          <template slot-scope="scope">
            {{ scope.row.type }}
          </template>
        </el-table-column>
        <el-table-column label="发布时间" min-width="40%" align="center">
          <template slot-scope="scope">
            {{ scope.row.reltime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="50%" align="center">
          <template slot-scope="{row}">
            <el-button type="primary" size="mini" @click="ShowCaseDetail(row)">查看</el-button>
            <el-button v-if="checkPermission(['校内老师', '企业导师'])" type="info" size="mini" @click="handleCaseEdit(row)">编辑</el-button>
            <el-button v-if="checkPermission(['校内老师', '企业导师'])" type="warning" size="mini" @click="handleCaseDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pagenum" :limit.sync="listQuery.pagesize" @pagination="fetchData" />
    </div>

    <!-- 案例详情对话框 -->
    <el-dialog title="案例信息" :visible.sync="dialogCaseVisible">
      <div>
        <!-- <el-form label-width="70px" style="width: 60%; margin-left: 50px">
          <el-form-item label="名称" style="height: 20px">{{ CaseDetail.name }}</el-form-item>
          <el-form-item label="类别" style="height: 20px">{{ CaseDetail.type }}</el-form-item>
          <el-form-item label="发布时间" style="height: 20px">{{ CaseDetail.reltime }}</el-form-item>
          <el-form-item label="附件" style="height: 20px">
            <el-link type="info" @click="downloadCaseFile">
              <span>点击下载</span>
            </el-link>
          </el-form-item>
        </el-form> -->
        <el-descriptions>
          <el-descriptions-item label="名称">{{ CaseDetail.name }}</el-descriptions-item>
          <el-descriptions-item label="类别">{{ CaseDetail.type }}</el-descriptions-item>
          <el-descriptions-item label="发布时间">{{ CaseDetail.reltime }}</el-descriptions-item>
          <el-descriptions-item label="附件">
            <el-link type="info" @click="downloadCaseFile">
              <span>点击下载</span>
            </el-link>
          </el-descriptions-item>
        </el-descriptions>
        <div style="margin-top: 10px">
          <span>正文内容:</span>
          <p v-html="CaseDetail.maintext">
            {{ CaseDetail.maintext }}
          </p>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Pagination from '@/components/Pagination'
import { queryCase, downloadCaseFile, deleteCase } from '@/api/case'
import checkPermission from '@/utils/permission'
import { downloadAttachs } from '@/utils/download'

export default {
  name: 'Case',
  components: { Pagination },
  data() {
    return {
      listQuery: {
        pagenum: 0, // 当前页
        pagesize: 10 // 每页数据条数
      },
      total: 1, // 数据总条数
      list: null, // 列表数据
      dialogCaseVisible: false,
      CaseDetail: {
        cid: '', // 案例编号
        name: '', // "案例名称"
        type: '', // "案例类别"
        maintext: '', // "案例正文"
        reltime: '', // 发布时间
        reluser: '', // 发布者
        attachment: '' // 附件
      }
    }
  },
  computed: {
    ...mapGetters([
      'role'
    ])
  },
  created() {
    this.fetchData()
  },
  methods: {
    checkPermission,
    fetchData() {
      queryCase(this.listQuery).then(res => {
        this.list = res.data.list
        this.total = res.data.total
      }).catch(() => {
        this.$message.error('获取案例列表错误')
      })
    },
    ShowCaseDetail(row) {
      this.CaseDetail = Object.assign({}, row)
      this.dialogCaseVisible = true
    },
    downloadCaseFile() {
      downloadCaseFile(this.CaseDetail.cid).then(res => {
        downloadAttachs(res)
        this.$message({
          message: '下载成功',
          type: 'success'
        })
      }).catch(() => {
        this.$message.error('下载失败')
      })
    },
    // 更新案例信息
    handleCaseEdit(row) {
      const cid = row.cid
      window.localStorage.setItem('cid', cid)
      this.$router.push('/case/edit')
    },
    // 删除案例
    handleCaseDelete(row) {
      deleteCase(row.cid).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '删除案例成功',
            type: 'success'
          })
          this.fetchData()
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.search-container {
  margin-left: 20px;
}
</style>
